<?php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;

class Topic extends Model
{
    // 指定表名
    protected $table = 'topics';

    // 允许批量赋值的字段
    protected $fillable = ['name'];

    // 禁止批量赋值的字段
    protected $guarded = [];

    // 属于专题的所有文章 [多对多]
    public function posts()
    {
        return $this->belongsToMany(Post::class, 'post_topics', 'topic_id', 'post_id');
    }

    // 获取专题的文章数[记录,用于withCount]
    public function postTopics()
    {
        return $this->hasMany(PostTopic::class, 'topic_id', 'id');
    }
}
